package com.treetao.union.db.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.treetao.union.db.Const.OrderRecord;
import com.treetao.union.db.Const.OrderStatus;
import com.treetao.union.db.mapper.MallPCommentMapper;
import com.treetao.union.db.model.MallOrderDetail;
import com.treetao.union.db.model.MallPComment;
import com.treetao.union.db.service.MallOrderDetailService;
import com.treetao.union.db.service.MallOrderService;
import com.treetao.union.db.service.MallPCommentService;

/**
 * <p>
 * 商品评论 服务实现类
 * </p>
 *
 * @author Dev.M
 * @since 2020-04-11
 */
@Service
public class MallPCommentServiceImpl extends ServiceImpl<MallPCommentMapper, MallPComment>
		implements MallPCommentService {
	
	@Autowired
	private MallOrderDetailService mallOrderDetailService;
	@Autowired
	private MallOrderService mallOrderService;

	@Transactional
	@Override
	public boolean saveComment(MallOrderDetail detail, Long userId, int star, String content) {
		
		MallPComment pc = new MallPComment();
		pc.setContent(content);
		pc.setStar(star);
		pc.setUserId(userId);
		
		pc.setSkuId(detail.getSkuId());
		pc.setSpuId(detail.getSpuId());
		pc.setSkuInfo(detail.getName()+"/"+detail.getAttrName());
		pc.setCommentTime(new Date());
		
		boolean flag1 = save(pc);
		
		detail.setCommented(true);
		boolean flag2 = mallOrderDetailService.updateById(detail);
		

		boolean finished = mallOrderDetailService.finishAllComment(detail.getOrderno());
		if(finished) {
			mallOrderService.changeOrderStatus(detail.getOrderno(), OrderStatus.COMMENTED, OrderRecord.COMMENTED);
		}
		return flag1 && flag2;
	}

	@Override
	public IPage<MallPComment> custPageDataRef(Page<?> page,QueryWrapper<?> q) {
		return baseMapper.custPageData(page, q);
	}


}
